<?php
namespace SisGG\FinalBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use SisGG\FinalBundle\Entity\Rol;
use SisGG\FinalBundle\Entity\Usuario;

class SecurityController extends Controller 
{    
    public function loginAction(){
        $peticion = $this->getRequest();
        $sesion = $peticion->getSession();
        if ($peticion->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
            $error = $peticion->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
        } else {
            $error = $sesion->get(SecurityContext::AUTHENTICATION_ERROR);
        }
        return $this->render('SisGGFinalBundle:Security:inicioSesion.html.twig',array('last_username' => $sesion->get(SecurityContext::LAST_USERNAME),'error'=> $error,));
    }
     
    public function usuarioNuevoAction(\Symfony\Component\HttpFoundation\Request $peticion){
            $usuario = new Usuario();
            $formulario=  $this->createFormBuilder($usuario)
                ->add('username',null,array('label' => 'Usuario'))
                ->add('password', 'repeated', array('type' => 'password',
                'invalid_message' => 'Los campos de contraseña deben coincidir.',
                'options' => array('label' => 'Contraseña'),
                ))  
                ->add('role',null,array('label' => 'Rol'))
                ->add('email','email',array('label' => 'Correo Electronico'))    
                ->getForm();
            if ($peticion->getMethod() == 'POST') {
             $formulario->bindRequest($peticion);
                if ($formulario->isValid()) {
                $usuario = $formulario->getData();
                $factory = $this->get('security.encoder_factory');
                $encoder = $factory->getEncoder($usuario);
                $password = $encoder->encodePassword($usuario->getPassword(), $usuario->getSalt());
                $usuario->setPassword($password);
                $em = $this->getDoctrine()->getEntityManager();
                $em->persist($usuario);
                $em->flush();
                return $this->render("SisGGFinalBundle:Administrador:nuevoUsuario.html.twig",array('mensaje'=>'El usuario'.$usuario->getUsername().'fue dado de alta.'));
            }
            }
            return $this->render("SisGGFinalBundle:Administrador:nuevoUsuario.html.twig",array("form" => $formulario->createView(),"legend"=>"Nuevo Usuario"));
      }
      
      public function rolNuevoAction(\Symfony\Component\HttpFoundation\Request $peticion){
            $role = new Rol();
            $formulario=  $this->createFormBuilder($role)
                ->add('role')
                ->getForm();
            if ($peticion->getMethod() == 'POST') {
             $formulario->bindRequest($peticion);
                if ($formulario->isValid()) {
                $role = $formulario->getData();
                $em = $this->getDoctrine()->getEntityManager();
                $em->persist($role);
                $em->flush(); 
                return $this->redirect($this->generateUrl('admin_principal',array('method'=>'POST','mensaje'=>'El rol'.$role->getRole().'fue dado de alta.')));
                }
            }
            return $this->render("SisGGFinalBundle:Administrador:nuevoRol.html.twig",array("form" => $formulario->createView()));
      }
      
    public function usuarioCreadoAction(){
        return $this->render('SisGGFinalBundle:Administrador:usuarioCreado.html.twig', array());
    }
    
    public function rolCreadoAction(){
        return $this->render('SisGGFinalBundle:Administrador:rolCreado.html.twig', array());
    }
}

